package com.example.myapplication.A_Test0515;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;


import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener{

    private Fg_1 fg1;
    private Fg_2 fg2;
    private Fg_3 fg3;
    private Fg_4 fg4;
    private List<Fragment> fglist = new ArrayList<>();
    private ViewPager vp_main;
    private RadioButton rb_1;
    private RadioButton rb_2;
    private RadioButton rb_3;
    private RadioButton rb_4;
    private RadioGroup rg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initFragment();
        initView();
        change1();
    }

    public void initFragment() {
        //初始化
        fg1 = new Fg_1();
        fg2 = new Fg_2();
        fg3 = new Fg_3();
        fg4 = new Fg_4();
        fglist.add(0, fg1);
        fglist.add(1, fg2);
        fglist.add(2, fg3);
        fglist.add(3, fg4);
    }
    public void change1() {
        rb_1.setTextColor(Color.parseColor("#EB2525"));
        rb_2.setTextColor(Color.parseColor("#707070"));
        rb_3.setTextColor(Color.parseColor("#707070"));
        rb_4.setTextColor(Color.parseColor("#707070"));
        rb_1.setChecked(true);
        //用drawable文件来实现
//        rb_home.setCompoundDrawablesWithIntrinsicBounds(null, top1_check, null, null);
//        rb_set.setCompoundDrawablesWithIntrinsicBounds(null, top2, null, null);
//        rb_hlep.setCompoundDrawablesWithIntrinsicBounds(null, top3, null, null);
        vp_main.setCurrentItem(0);
        //回调
        //fg_home.setData(dataCallBack);
    }

    public void change2() {
        rb_1.setTextColor(Color.parseColor("#707070"));
        rb_2.setTextColor(Color.parseColor("#EB2525"));
        rb_3.setTextColor(Color.parseColor("#707070"));
        rb_4.setTextColor(Color.parseColor("#707070"));
        rb_2.setChecked(true);
        vp_main.setCurrentItem(1);
    }

    public void change3() {
        rb_1.setTextColor(Color.parseColor("#707070"));
        rb_2.setTextColor(Color.parseColor("#707070"));
        rb_3.setTextColor(Color.parseColor("#EB2525"));
        rb_4.setTextColor(Color.parseColor("#707070"));
        rb_3.setChecked(true);
        vp_main.setCurrentItem(2);
    }

    public void change4() {
        rb_1.setTextColor(Color.parseColor("#707070"));
        rb_2.setTextColor(Color.parseColor("#707070"));
        rb_3.setTextColor(Color.parseColor("#707070"));
        rb_4.setTextColor(Color.parseColor("#EB2525"));
        rb_4.setChecked(true);
        vp_main.setCurrentItem(3);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_1:
                vp_main.setCurrentItem(0);
                break;
            case R.id.rb_2:
                vp_main.setCurrentItem(1);
                break;
            case R.id.rb_3:
                vp_main.setCurrentItem(2);
                break;
            case R.id.rb_4:
                vp_main.setCurrentItem(3);
                break;
        }
    }

    private void initView() {
        vp_main = (ViewPager) findViewById(R.id.vp_main);
        VpAdapter vpAdapter = new VpAdapter(getSupportFragmentManager(),0, fglist);
        vp_main.setAdapter(vpAdapter);
        vp_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        change1();
                        break;
                    case 1:
                        change2();
                        break;
                    case 2:
                        change3();
                        break;
                    case 3:
                        change4();
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rb_1 = (RadioButton) findViewById(R.id.rb_1);
        rb_1.setOnClickListener(this);
        rb_2 = (RadioButton) findViewById(R.id.rb_2);
        rb_2.setOnClickListener(this);
        rb_3 = (RadioButton) findViewById(R.id.rb_3);
        rb_3.setOnClickListener(this);
        rb_4 = (RadioButton) findViewById(R.id.rb_4);
        rb_4.setOnClickListener(this);
        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnClickListener(this);
    }


    class VpAdapter extends FragmentPagerAdapter {

        private List<Fragment> list;

        public VpAdapter(@NonNull FragmentManager fm, int behavior, List<Fragment> list) {
            super(fm,behavior);
            this.list = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return (list != null) ? list.size() : 0;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.destroyItem(container, position, object);
        }
    }

}
